﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Vit.Common;
using Vit.Web;
using Vit.Web.Models;
using Vit.Web.Providers;
using Vit.Web.Mvc.Results;

namespace Vit.AdminModule.Areas.Admin.Controllers
{
    public class UserController : Vit.Web.Mvc.Controllers.DataControllerBase<Vit.Web.Models.User>
    {
        [HttpGet]
        public ActionResult Roles(string id)
        {
            object[] keys = UnFormatKeys(id);
            var user = this.DataProvider.GetByKeys(keys);
            var roleDataProvider = GetDataProvider<Role>(null);
            var userRoles = user == null ? Enumerable.Empty<Role>() : user.Roles.ToList();
            var roles = roleDataProvider.GetAll().Select(o => new SelectListItem() { Text = o.Name, Value = o.Name }).ToList();
            foreach (var r in roles)
            {
                if (userRoles.Any(o => o.Name == r.Value))
                    r.Selected = true;
            }
            ViewBag.User = user;
            return View(roles);
        }

        [HttpPost]
        public ActionResult Roles(FormCollection form)
        {
            object[] keys = UnFormatKeys(form["UserID"]);
            var user = this.DataProvider.GetByKeys(keys);
            if (user != null)
            {
                user.Roles.Clear();
                var roleDataProvider = GetDataProvider<Role>(null);
                var selectedValues = form["SelectedValues"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string val in selectedValues)
                {
                    object[] ids = UnFormatKeys(val);
                    Role role = roleDataProvider.GetByKeys(ids);
                    if (role != null)
                    {
                        user.Roles.Add(role);
                    }
                }
                this.DataProvider.Modify(user);
            }
            return Refresh();
        }

        [HttpGet]
        public ActionResult AccountExists(string account)
        {
            bool exists = false;
            try
            {
                exists = this.DataProvider.GetAll().Any(o => o.Account == account);
            }
            catch { }
            return Json(!exists, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult EmailExists(string email, string account)
        {
            bool exists = false;
            try
            {
                var user = this.DataProvider.GetAll().FirstOrDefault(o => o.Account == account);
                if (user != null && user.Email == email)
                    return Json(true, JsonRequestBehavior.AllowGet);
                exists = this.DataProvider.GetAll().Any(o => o.Email == email);
            }
            catch { }
            return Json(!exists, JsonRequestBehavior.AllowGet);
        }
    }
}
